package com.nf.health.app.widget;


import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.nf.health.app.R;

import static com.nf.health.app.R.*;


/**
 * Created by yuyahao
 */
public class ShowDialogFactory extends Dialog {
    private WindowManager wm;
    private LayoutInflater inflate;
    private View view;//加载的布局
    private Display d;
    private RelativeLayout rl_content;
    private WindowManager.LayoutParams p;
    private LinearLayout ll_llcontextmsg;
    private Context context;
    private ImageView iv_iamgeView;
    public ImageButton  img_dialog_entry;
    private String url;
//    private MyCountDownTimer mc;

    public ShowDialogFactory(Context context) {
        super(context, R.style.dialog3);
        init(context);
    }


    public ShowDialogFactory(Context context, AttributeSet attrs) {
        super(context,R.style.dialog3);
        // TODO Auto-generated constructor stub
        init(context);
    }

    public ShowDialogFactory(Context context, AttributeSet attrs, int defStyle) {
        super(context, R.style.dialog3);
        // TODO Auto-generated constructor stub
        init(context);
    }


    /****
     * c初始化数据
     ******/
    private void init(Context context) {
        this.context = context;
        setContentView(R.layout.dialog_imageview);
        iv_iamgeView = (ImageView) findViewById(id.iv_iamgeView);
        rl_content = (RelativeLayout) findViewById(id.rl_content);
        img_dialog_entry = (ImageButton) findViewById(id.img_dialog_entry);
        setCircleDialogAtter(0.85, 0.75);
//        mc = new MyCountDownTimer(3000, 1);//倒计时等待时间
    }

    /***
     * 设置dialog的宽高，带有背景黑色的
     *****/
    public void setCircleDialogAtter(double widthValue, double heithValue) {
        ViewGroup.LayoutParams para = rl_content.getLayoutParams();
        WindowManager m = getWindow().getWindowManager();
        d = m.getDefaultDisplay();  //为获取屏幕宽、高
        p = getWindow().getAttributes();  //获取对话框当前的参数值
        para.height = (int) (d.getHeight() * heithValue);
        ;
        para.width = (int) (d.getWidth() * widthValue);
        rl_content.setLayoutParams(para);

    }

    /*
     设置dialog的大小和位置
    */
    public void setDialogHigeht(double size) {
        p.height = (int) (d.getHeight() * size);
        p.width = (int) (d.getWidth() * 0.8);    //宽度设置为屏幕的0.8
        p.alpha = 1.0f;      //设置本身透明度
        p.dimAmount = 0.0f;      //设置黑暗度

        getWindow().setAttributes(p);     //设置生效
        getWindow().setGravity(Gravity.CENTER);       //设置对齐
    }

    public void setImageBitmapResouble(String url) {
        this.url = url;
        /*Glide.with(context)
                .load(url)
                .into(iv_iamgeView);*/
        //在使用Glide加载网络图片时，通常需要知道下载成功或失败的回调，使用方法是：
        setThumbnail(url);
    }

    public void setThumbnail(String url) {
        if (iv_iamgeView != null) {
            iv_iamgeView.setScaleType(ImageView.ScaleType.CENTER);
            Glide.with(context).
                    load(url).
                    animate(android.R.anim.fade_in).
                    into(new GlideDrawableImageViewTarget(iv_iamgeView) {
                             @Override
                             public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                                 super.onResourceReady(resource, animation);
                                 iv_iamgeView.setScaleType(ImageView.ScaleType.FIT_XY);
                                 iv_iamgeView.setImageDrawable(resource);

//                                 mc.start();
//                                 Drawable drawable =  context.getResources().getDrawable(R.drawable.dialog_cancel); //不会变形
                                 img_dialog_entry.setBackgroundResource(drawable.dialog_cancel);
                             }

                             @Override
                             public void onStop() {
                                 super.onStop();
                             }

                             @Override
                             public void onLoadCleared(Drawable placeholder) {
                                 super.onLoadCleared(placeholder);
                             }
                         }
                    );
        }
    }

    public void setDdalog_entry(View.OnClickListener listener) {
        img_dialog_entry.setOnClickListener(listener);
    }

    public void setDdalog_Imgentry(View.OnClickListener listener) {
        iv_iamgeView.setOnClickListener(listener);
    }

    public void setDdalogVisibile(int visisbile) {
        img_dialog_entry.setVisibility(visisbile);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        dismiss();
    }

    /**
     * 继承 CountDownTimer 防范
     * <p/>
     * 重写 父类的方法 onTick() 、 onFinish()
     */

    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p/>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p/>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {
            dismiss();
        }
        @Override
        public void onTick(long millisUntilFinished) {

            int time = (int) millisUntilFinished / 1000;
//            img_dialog_entry.setText(" "+time+"  s跳过");
        }
    }

}
